<template>
  <DxScrollView width="100%" height="100%">
    <DxForm
      label-location="left"
      :col-count="1"
      :form-data="specificationPriceModel"
    >
      <DxGroupItem caption="规格价格信息" :col-count="3">
        <DxSimpleItem data-field="attributes" editorType="dxSelectBox" :editor-options="attributesOptions">
          <DxLabel text="药价属性"
        /></DxSimpleItem>
        <DxSimpleItem
          data-field="guideDifference"
          :editor-options="getSimpleSuffix('%')"
        >
          <DxLabel text="指导差率" />
        </DxSimpleItem>
        <DxSimpleItem
          data-field="biddingPrice"
          :editor-options="getSimpleSuffix('元/支')"
        >
          <DxLabel text="中标价格" />
        </DxSimpleItem>
        <DxSimpleItem data-field="costPrice" :editor-options="editOptions">
          <DxLabel text="成本价格" />
        </DxSimpleItem>
        <DxSimpleItem
          data-field="markupRate"
          :editor-options="getSimpleSuffix('%')"
        >
          <DxLabel text="加成率" />
        </DxSimpleItem>
        <DxSimpleItem
          data-field="purchaseDeductionRate"
          :editor-options="getSimpleSuffix('%')"
        >
          <DxLabel text="采购扣率" />
        </DxSimpleItem>
        <DxSimpleItem
          data-field="currentSellingPrice"
          :editor-options="{
            disabled: true,
            stylingMode: 'outlined',
          }"
        >
          <DxLabel text="当前售价" />
        </DxSimpleItem>
        <DxSimpleItem
          data-field="managementFeeRatio"
          :editor-options="getSimpleSuffix('%')"
        >
          <DxLabel text="管理费比率" />
        </DxSimpleItem>
        <DxSimpleItem
          data-field="settlementPrice"
          :editor-options="
            getSimpleSuffix('元/支', {
              disabled: true,
            })
          "
        >
          <DxLabel text="结算价" />
        </DxSimpleItem>
        <DxSimpleItem
          data-field="feeDiscount"
          editorType="dxSelectBox"
          :editor-options="feeDiscountOptions" 
        >
          <DxLabel text="费别折扣"
        /></DxSimpleItem>
        <DxSimpleItem
          data-field="vatRate"
          :editor-options="getSimpleSuffix('%')"
        >
          <DxLabel text="增值税率" />
        </DxSimpleItem>
        <DxSimpleItem
          data-field="guidePrice"
          :editor-options="
            getSimpleSuffix('元/支')
          "
        >
          <DxLabel text="指导售价" />
        </DxSimpleItem>
        <DxSimpleItem
          data-field="priceDifference"
          :editor-options="priceDifferenceOptions"
        >
          <DxLabel text="差价让利" />
        </DxSimpleItem>
      </DxGroupItem>
      <DxGroupItem caption="规格财务信息" :col-count="3">
        <DxSimpleItem
          data-field="incomeItem"
          editorType="dxSelectBox"
          :editor-options="incomeItemOptions"
        >
          <DxLabel text="收入项目"
        /></DxSimpleItem>
        <DxSimpleItem
          data-field="drugPriceLevel"
          editorType="dxSelectBox"
          :editor-options="drugPriceLevelOptions"
        >
          <DxLabel text="药价级别"
        /></DxSimpleItem>
        <DxSimpleItem
          data-field="typeOfBaseDrug"
          editorType="dxSelectBox"
          :editor-options="typeOfBaseDrugOptions"
        >
          <DxLabel text="基药类型"
        /></DxSimpleItem>
        <DxSimpleItem
          data-field="medicalRecordCost"
          editorType="dxSelectBox"
          :editor-options="medicalRecordCostOptions"
        >
          <DxLabel text="病案项目"
        /></DxSimpleItem>
        <DxSimpleItem
          data-field="drugInsurance"
          editorType="dxSelectBox"
          :editor-options="drugInsuranceOptions"
        >
          <DxLabel text="医保类型"
        /></DxSimpleItem>
      </DxGroupItem>
      <DxGroupItem caption="规格使用信息" :col-count="3">
        <DxSimpleItem
          data-field="serviceObject"
          editorType="dxSelectBox"
          :editor-options="serviceObjectOptions"
        >
          <DxLabel text="服务对象"
        /></DxSimpleItem>
        <DxSimpleItem
          data-field="zeroOutpatient"
          editorType="dxSelectBox"
          :editor-options="zeroOutpatientOptions"
        >
          <DxLabel text="门诊分零"
        /></DxSimpleItem>
        <DxSimpleItem
          data-field="beHospitalizedDivideZero"
          editorType="dxSelectBox"
          :editor-options="beHospitalizedDivideZeroOptions"
        >
          <DxLabel text="住院分零"
        /></DxSimpleItem>
      </DxGroupItem>
    </DxForm>
  </DxScrollView>
</template>

<script lang="ts">
import { DxPopup } from "devextreme-vue/popup";
import {
  DxForm,
  DxSimpleItem,
  DxGroupItem,
  DxTabbedItem,
  DxTabPanelOptions,
  DxTab,
  DxRequiredRule,
  DxLabel,
} from "devextreme-vue/form";
import DxTagBox from "devextreme-vue/tag-box";
import DxCheckBox from "devextreme-vue/check-box";
import { Vue, Options, Prop, Watch, Emit } from "vue-property-decorator";
import { DxScrollView } from "devextreme-vue/scroll-view";
import { DxColumn, DxDataGrid, DxEditing } from "devextreme-vue/data-grid";
import DataSource from "devextreme/data/data_source";
import DxButton from "devextreme-vue/button";
import { dxSelectBoxOptions } from "devextreme/ui/select_box";
import {
  DictionaryItemModel,
  SpecificationPriceModel,
} from "@/api/api_compatibilitytaboodrugclient";
import {
  DrugDrugMedicalPosition,
  DrugSpecificationMedicineType,
  DrugSpecificationPriceBeHospitalizedDivideZero,
  DrugSpecificationPriceDrugInsurance,
  DrugSpecificationPriceDrugPriceLevel,
  DrugSpecificationPriceIncomeItem,
  DrugSpecificationPriceMedicalRecordCost,
  DrugSpecificationPricePriceDifference,
  DrugSpecificationPriceServiceObject,
  DrugSpecificationPriceTypeOfBaseDrug,
  DrugSpecificationPriceZeroOutpatient,
} from "@/config/field-const-name";
import { LOGINPATH } from "../../../utils/basic-data";
let self: Prise;
@Options({
  components: {
    DxPopup,
    DxForm,
    DxSimpleItem,
    DxGroupItem,
    DxTabbedItem,
    DxTabPanelOptions,
    DxTab,
    DxRequiredRule,
    DxLabel,
    DxTagBox,
    DxCheckBox,
    DxScrollView,
    DxColumn,
    DxDataGrid,
    DxEditing,
    DxButton,
  },
})
export default class Prise extends Vue {
  // value 输入输出
  @Prop()
  value: SpecificationPriceModel;
  @Prop({ default: {} })
  dictionaryList: DictionaryItemModel;
  @Emit("update:value")
  setValue(e) {}

  get specificationPriceModel() {
    return this.value;
  }
  set specificationPriceModel(value) {
    this.setValue(value);
  }

  editOptions = {
    stylingMode: "outlined",
  };
  // 保留小数点后六位
  fixSix(num, decimal) {
    num = num.toString();
    let index = num.indexOf(".");
    if (index !== -1) {
      num = num.substring(0, decimal + index + 1);
    } else {
      num = num.substring(0);
    }
    return parseFloat(num).toFixed(decimal);
  }
  daa(e) {
    let eleE = e.element.innerHTML.indexOf('">');
    let eleS = e.element.innerHTML.indexOf('name="') + 6;
    let watchD = e.element.innerHTML.substring(eleS, eleE);
    let a; //a 是公式a
    if (watchD == "guideDifference") {
      this.specificationPriceModel.markupRate = Number(
        this.fixSix((1 / (1 - e.event.target.value / 100) - 1) * 100, 6)
      );
      this.specificationPriceModel = { ...this.specificationPriceModel };
    } else if (watchD == "markupRate") {
      let a = Number(e.event.target.value / 100);
      this.specificationPriceModel.guideDifference = Number(
        this.fixSix((a / (a + 1)) * 100, 6)
      );
      this.specificationPriceModel = { ...this.specificationPriceModel };
    }
    if (!this.specificationPriceModel.costPrice) {
      return;
    } else {
      a =
        this.specificationPriceModel.costPrice *
        (1 + this.specificationPriceModel.markupRate / 100);
    }
    if (a < this.specificationPriceModel.guidePrice) {
      this.specificationPriceModel.currentSellingPrice = Number(
        this.fixSix(
          a +
            (this.specificationPriceModel.guidePrice - a) *
              (1 - this.specificationPriceModel.priceDifference),
          6
        )
      );
    } else {
      this.specificationPriceModel.currentSellingPrice =
        this.specificationPriceModel.guidePrice;
    }
    this.specificationPriceModel = { ...this.specificationPriceModel };
  }
  getSimpleSuffix(
    text: string,
    extendOptions: dxSelectBoxOptions
  ): dxSelectBoxOptions {
    let options: dxSelectBoxOptions = {
      stylingMode: "outlined",
      buttons: [
        {
          name: "unit",
          location: "after",
          options: {
            text: text,
            stylingMode: "text",
          },
        },
      ],
      onChange(e) {
        let event = e as any;
        self.daa(event);
      },
    };

    return Object.assign({}, options, extendOptions);
  }
  tagSetting: dxSelectBoxOptions = {
    stylingMode: "outlined",
    displayExpr: "displayValue",
    valueExpr: "dictionaryId",
    searchEnabled: true,
    searchExpr: "value",
    showDropDownButton: true,
  };

  getTagSetting(items: any[]): dxSelectBoxOptions {
    return {
      ...this.tagSetting,
      items: items || [],
    };
  }

  // 药价属性
  get attributesOptions() {
    return this.getTagSetting(
      this.dictionaryList.drugSpecificationPriceAttributes || []
    );
  }
  // 收入项目
  get incomeItemOptions() {
    return this.getTagSetting(
      this.dictionaryList[DrugSpecificationPriceIncomeItem] || []
    );
  }
  // 药价级别
  get drugPriceLevelOptions() {
    return this.getTagSetting(
      this.dictionaryList["drugSpecificationPriceDrugPriceLevel"] || []
    );
  }
  // 基药类型
  get typeOfBaseDrugOptions() {
    return this.getTagSetting(
      this.dictionaryList["drugSpecificationPriceTypeOfBaseDrug"] || []
    );
  }
  // 病案费用
  get medicalRecordCostOptions() {
    return this.getTagSetting(
      this.dictionaryList[DrugSpecificationPriceMedicalRecordCost] || []
    );
  }
  // 医保类型????
  get drugInsuranceOptions() {
    return this.getTagSetting(
      this.dictionaryList.drugSpecificationPriceDrugInsurance || []
    );
  }
  // 服务对象
  get serviceObjectOptions() {
    return this.getTagSetting(
      this.dictionaryList.drugSpecificationPriceServiceObject || []
    );
  }

  // 门诊分零
  get zeroOutpatientOptions() {
    return this.getTagSetting(
      this.dictionaryList[DrugSpecificationPriceZeroOutpatient] || []
    );
  }
  // 住院分零
  get beHospitalizedDivideZeroOptions() {
    return this.getTagSetting(
      this.dictionaryList[DrugSpecificationPriceBeHospitalizedDivideZero] || []
    );
  }
  // 差价让利
  get priceDifferenceOptions() {
    return this.getTagSetting(
      this.dictionaryList[DrugSpecificationPricePriceDifference] || []
    );
  }
  // 费别折扣
  get feeDiscountOptions() {
    //console.log(' this.dictionaryList.drugSpecificationPriceFeeDiscount', this.dictionaryList.drugSpecificationPriceFeeDiscount);
    console.log('this.dictionaryList',this.dictionaryList);
    return this.getTagSetting(
      this.dictionaryList.drugSpecificationPriceFeeDiscount || []
    );
  }
  dataSource = [];

  created() {
    self = this;
  }
}
</script>

<style lang="scss" scoped>
.button-group {
  bottom: 0;
  display: flex;
  width: 100%;
  padding-right: 20px;
  justify-content: flex-end;

  .button-bar {
    margin-top: 20px;
    width: 40%;
    display: flex;
    justify-content: space-between;
  }
}
.title {
  height: 56px;
  padding: 15px;
  .title-content {
    display: flex;
    justify-content: space-between;

    .title-operation {
      display: flex;
      justify-content: space-between;
    }
  }
}
</style>
